<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px">
      <el-form-item label="EN-Name" prop="enName">
        <el-input
          v-model="queryParams.enName"
          placeholder="English Name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="CN-Name" prop="cnName">
        <el-input
          v-model="queryParams.cnName"
          placeholder="Chinese Name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="行业编号" prop="indCode">-->
        <!--<el-input-->
          <!--v-model="queryParams.indCode"-->
          <!--placeholder="请输入行业编号"-->
          <!--clearable-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="其他详情" prop="otherDetail">-->
        <!--<el-input-->
          <!--v-model="queryParams.otherDetail"-->
          <!--placeholder="请输入其他详情"-->
          <!--clearable-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item
        label-width="150px"
        label="Industry Detail"
        prop="industryDetail">
        <el-input
          v-model="queryParams.industryDetail"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item
        label-width="100px"
        label="Head Count"
        prop="userCount">
        <el-input
          style="width: 100px;margin-right: 10px;"
          v-model="queryParams.minCount"
          placeholder="From"
          clearable
          @keyup.enter.native="handleQuery"
        />
        <span>-</span>
        <el-input
          style="width: 100px;margin-left: 10px;"
          v-model="queryParams.maxCount"
          placeholder="To"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <!--<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >Add</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['system:cmp:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
          <!--v-hasPermi="['system:cmp:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >Import</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="warning"-->
          <!--plain-->
          <!--icon="el-icon-download"-->
          <!--size="mini"-->
          <!--@click="handleExport"-->
          <!--v-hasPermi="['system:cmp:export']"-->
        <!--&gt;导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="cmpList"
      @selection-change="handleSelectionChange">
      <el-table-column
        width="70px"
        label="Action"
        align="center"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >Edit</el-button>
          <!--<el-button-->
          <!--size="mini"-->
          <!--type="text"-->
          <!--icon="el-icon-delete"-->
          <!--@click="handleDelete(scope.row)"-->
          <!--&gt;删除</el-button>-->
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="EN-Name"
        align="left"
        prop="enName" />
      <el-table-column
        :show-overflow-tooltip="true"
        label="CN-Name"
        align="left"
        prop="cnName" />
      <el-table-column
        width="110px"
        :show-overflow-tooltip="true"
        label="Industry Code"
        align="left"
        prop="indCode" />
      <el-table-column
        :show-overflow-tooltip="true"
        label="Head Count"
        align="left"
        prop="userCount" />
      <el-table-column
        :show-overflow-tooltip="true"
        label="Other Details"
        align="left"
        prop="otherDetail" />
      <el-table-column
        :show-overflow-tooltip="true"
        label="Industry Detail"
        align="left"
        prop="industryDetail" />
      <!--<el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="更新时间" align="center" prop="updateTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="创建人" align="center" prop="createUserId" />-->
      <!--<el-table-column label="更新人" align="center" prop="updateUserId" />-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog title="Company" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="English Name" prop="enName">
          <el-input v-model="form.enName" placeholder="" />
        </el-form-item>
        <el-form-item label="Chinese Name" prop="cnName">
          <el-input v-model="form.cnName" placeholder="" />
        </el-form-item>
        <!--<el-form-item label="Industry Code" prop="indCode">-->
          <!--<el-input v-model="form.indCode" placeholder="" />-->
        <!--</el-form-item>-->
        <el-form-item
          prop="indCode"
          label="Industry Code">
          <el-select
            clearable
            filterable
            remote
            :remote-method="filterVmModel"
            v-selectLoadmore="loadMore"
            v-model="form.indCode"
            placeholder="">
            <el-option
              v-for="item in userOptions"
              :key="item.indCode"
              :label="item.indCode"
              :value="item.indCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Other Details" prop="otherDetail">
          <el-input v-model="form.otherDetail" placeholder="" />
        </el-form-item>
        <el-form-item label="2021 Dec Head Count" prop="userCount">
          <el-input disabled v-model="form.userCount" placeholder="" />
        </el-form-item>
        <el-form-item label="Updated Head Count" prop="latestCount">
          <el-input
            type="text"
            v-model="form.latestCount"
            placeholder="" />
        </el-form-item>
        <el-form-item label="Contact No." prop="phone">
          <el-input
            type="text"
            v-model="form.phone"
            placeholder="" />
        </el-form-item>
        <el-form-item label="Contact Person" prop="contact">
          <el-input v-model="form.contact" placeholder="" />
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input v-model="form.email" placeholder="" />
        </el-form-item>
        <el-form-item label="Address" prop="address">
          <el-input rows="3" type="textarea" v-model="form.address" placeholder="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">Confirm</el-button>
        <el-button @click="cancel">Cancel</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">Drag the file here,OR<em>Click to upload</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> Update existing user data?
          </div>
          <span>Only format xls、xlsx</span>
          <!--<el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>-->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">Confirm</el-button>
        <el-button @click="upload.open = false">Cancel</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCmp, getCmp, delCmp, addCmp, updateCmp } from "@/api/system/cmp";
import { listIndustry } from "@/api/system/industry";
import { getToken } from "@/utils/auth";

export default {
  name: "Cmp",
  data() {
    return {
      loadingStaff: false,
      userOptions: [],
      userList: [],
      userTotal: 0,
      queryParamsUser: {
        pageNum: 1,
        pageSize: 10,
        indCode: null
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/cmp/importData"
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      cmpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enName: null,
        cnName: null,
        indCode: null,
        otherDetail: null,
        userCount: null,
        createTime: null,
        updateTime: null,
        createUserId: null,
        updateUserId: null,
        minCount: null,
        maxCount: null,
        industryDetail: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  mounted() {
    // 禁用复制
    // this.$nextTick(() => {
    //   document.onselectstart = new Function("event.returnValue=false");
    // })
  },
  created() {
    this.getList();
    this.getIncCodeList();
  },
  methods: {
    loadMore(firstTag) {
      this.queryParamsUser.pageNum++;
      this.getIncCodeList();
    },
    filterVmModel (value) {
      this.loadingStaff = true;
      console.log("=======================" + value)
      this.queryParamsUser.indCode = value;
      this.queryParamsUser.pageNum = 1;
      this.getIncCodeList();
    },
    getIncCodeList() {
      this.loadingStaff = true;
      listIndustry(this.queryParamsUser).then(response => {
        // this.userOptions = response.rows;
        // this.userTotal = response.total;
        // this.loadingStaff = false;
        this.userList = response.rows;
        if (this.queryParamsUser.pageNum == 1) {
          this.userOptions = [];
          this.userOptions.push(...this.userList);
        } else {
          this.userOptions.push(...this.userList);
        }
        this.userTotal = response.total;

      }).finally(f => {
        this.loadingStaff = false;
      });
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "import";
      this.upload.open = true;
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listCmp(this.queryParams).then(response => {
        this.cmpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enName: null,
        cnName: null,
        indCode: null,
        otherDetail: null,
        userCount: null,
        createTime: null,
        updateTime: null,
        createUserId: null,
        updateUserId: null,
        latestCount: null,
        phone: null,
        contact: null,
        email: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      // this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCmp(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCmp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCmp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delCmp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cmp/export', {
        ...this.queryParams
      }, `cmp_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
